package com.webex.ta.hydra.gui.wm;

import java.util.Stack;
import java.util.Iterator;

/**
 * Author: Vegaz
 * Date: 2010-11-2
 * Time: 14:05:33
 */
public class SideStack {
    private final Stack<WindowInfo> stack;

    public SideStack() {
        stack = new Stack<WindowInfo>();
    }

    public void push(final WindowInfo info) {
        stack.push(info);
    }

    public WindowInfo pop(final int anchor) {
        for (int i = stack.size() - 1; i >= 0; i--) {
            final WindowInfo info = stack.get(i);
            if (anchor == info.getAnchor()) {
                stack.remove(i);
                return info;
            }
        }
        return null;
    }

    public WindowInfo peek(final int anchor) {
        for (int i = stack.size() - 1; i >= 0; i--) {
            final WindowInfo info = stack.get(i);
            if (anchor == info.getAnchor()) {
                return info;
            }
        }
        return null;
    }

    boolean isEmpty(final int anchor) {
        for (int i = stack.size() - 1; i > -1; i--) {
            final WindowInfo info = stack.get(i);
            if (anchor == info.getAnchor()) {
                return false;
            }
        }
        return true;
    }

    void remove(final String id) {
        for (Iterator<WindowInfo> i = stack.iterator(); i.hasNext();) {
            final WindowInfo info = i.next();
            if (id.equals(info.getId())) {
                i.remove();
            }
        }
    }

    public void clear() {
        stack.clear();
    }

    public int size() {
        return stack.size();
    }
}
